﻿using System;
using System.IO;
using System.Text;

namespace ZFramework.Runtime
{
    public class ByteBuffer {
        public MemoryStream stream = null;
        public BinaryWriter writer = null;
        public BinaryReader reader = null;

        public static ByteBuffer Create()
        {
            return new ByteBuffer();
        }

        public ByteBuffer() {
            stream = new MemoryStream();
            writer = new BinaryWriter(stream);
            reader = new BinaryReader(stream);
        }

        public ByteBuffer(byte[] data) {
            stream = new MemoryStream(data);
            writer = new BinaryWriter(stream);
            reader = new BinaryReader(stream);
        }

        public void Close() {
            writer?.Close();
            reader?.Close();
            stream?.Close();
            writer = null;
            reader = null;
            stream = null;
        }

        public void WriteByte(byte v) {
            writer.Write(v);
        }

        public void WriteInt(int v)
        {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }
        
        public void WriteUInt(int v)
        {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }

        public void WriteShort(short v) {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }

        public void WriteUShort(ushort v)
        {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }

        public void WriteLong(long v)
        {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }

        public void WriteFloat(float v) {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }

        public void WriteDouble(double v) {
            byte[] buf = BitConverter.GetBytes(v);
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(buf);
            writer.Write(buf);
        }

        public void WriteString(string v) {
            byte[] bytes = Encoding.UTF8.GetBytes(v);
            WriteUShort((ushort)bytes.Length);
            writer.Write(bytes);
        }
        
        public void WriteRawBytes(byte[] v) {
            writer.Write(v);
        }

        public void WriteBytes(byte[] v) {
            WriteUInt(v.Length);
            writer.Write(v);
        }

        public byte ReadByte() {
            return reader.ReadByte();
        }

        public int ReadInt32()
        {
            var data = BitConverter.GetBytes(reader.ReadInt32());
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(data);
            return BitConverter.ToInt32(data, 0);
        }

        public short ReadShort() {
            var data = BitConverter.GetBytes(reader.ReadInt16());
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(data);
            return BitConverter.ToInt16(data, 0);
        }

        public ushort ReadUShort() {
            var data = BitConverter.GetBytes(reader.ReadUInt16());
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(data);
            return BitConverter.ToUInt16(data, 0);
        }

        public long ReadLong() {
            var data = BitConverter.GetBytes(reader.ReadInt64());
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(data);
            return BitConverter.ToInt64(data, 0);
        }

        public float ReadFloat() {
            byte[] temp = BitConverter.GetBytes(reader.ReadSingle());
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(temp);
            return BitConverter.ToSingle(temp, 0);
        }

        public double ReadDouble() {
            byte[] temp = BitConverter.GetBytes(reader.ReadDouble());
            if (BitConverter.IsLittleEndian) // 如果系统是小端字节序，则反转数组来获得大端字节序
                Array.Reverse(temp);
            return BitConverter.ToDouble(temp, 0);
        }

        public string ReadString() {
            ushort len = ReadUShort();
            var buffer = reader.ReadBytes(len);
            return Encoding.UTF8.GetString(buffer);
        }

        public byte[] ReadBytes() {
            int len = ReadInt32();
            return reader.ReadBytes(len);
        }

        public byte[] ReadBytes(int len)
        {
            return reader.ReadBytes(len);
        }

        public byte[] ToBytes() {
            writer.Flush();
            return stream.ToArray();
        }

        public void Flush() {
            writer.Flush();
        }
    }
}